
package com.maven.front.service.frontSubject;

import com.maven.front.bean.EduCourseStudyhistory;
import com.maven.front.mapper.EduCourseStudyhistoryMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;


/**
 * @program: web-front
 * @description: 视频播放记录
 * @author: WangYiLong
 * @create: 2020-08-15 15:51
 **/

@Service
@Slf4j
public class VideoService {

    @Resource
    private EduCourseStudyhistoryMapper eduCourseStudyhistoryMapper;

    public int updateStudyHistory(EduCourseStudyhistory studyHistory){
        EduCourseStudyhistory eduCourseStudyhistory = eduCourseStudyhistoryMapper.selectByUserIdAndKpointId(studyHistory);
        log.info("*************"+eduCourseStudyhistory.toString());
        //若数据库无此章节学习记录，则执行插入操作
        if(ObjectUtils.isEmpty(eduCourseStudyhistory)){
            studyHistory.setUpdateTime(new Date());
            studyHistory.setPlayercount(1);
          int num1 =   eduCourseStudyhistoryMapper.insertSelective(studyHistory);
          return num1;
        }
        log.info("**********观看时间:"+studyHistory.getCurrentTime());
        //记录最大观看时间
        if(studyHistory.getCurrentTime()>(eduCourseStudyhistory.getMaxTime()==null?0:eduCourseStudyhistory.getMaxTime())){
            studyHistory.setMaxTime(studyHistory.getCurrentTime());
        }
        //否则，更新该记录
        return eduCourseStudyhistoryMapper.updateByPrimaryKeySelective(studyHistory);
    }

    public EduCourseStudyhistory getStudyHistory(EduCourseStudyhistory history){
        EduCourseStudyhistory courseStudyhistory = eduCourseStudyhistoryMapper.selectByUserIdAndKpointId(history);
        if(ObjectUtils.isEmpty(courseStudyhistory)){
            history.setUpdateTime(new Date());
            history.setPlayercount(1);
            history.setCurrentTime(0);
            history.setMaxTime(0);
            eduCourseStudyhistoryMapper.insertSelective(history);
            return eduCourseStudyhistoryMapper.selectByPrimaryKey(history.getId());
        }
        return courseStudyhistory;
    }
}

